/**
 * 计算性能评分:
 * 时间维度（50分）
 * 页面维度(50分)
 */
function calcScore(performance){
    let timeScore = timeScoreCalc(performance.performance, performance.lighthouse.audits.timing)
    let sourceScore = sourceScoreCalc(performance.lighthouse.audits.opportunity);
    return {
        totalScore: timeScore.score + sourceScore.score,
        timeScore,
        sourceScore
    }
}

const baseScoreWithAudit = {
    "First Contentful Paint": 7,
    "Largest Contentful Paint": 7,
    "First Meaningful Paint": 7,
    "Estimated Input Latency": 7,
    "Total Blocking Time": 7,
    "Total load time": 7,
    "DNS resolution time consuming": 4,
    "TCP connection time consuming": 4,

}
// 时间维度计分
function timeScoreCalc(timing, auditTime){
    let initScore = 0;
    let scoreDetail = Object.create(baseScoreWithAudit);
    // dns耗时: 100ms为基准，每超出10ms扣一分,总共4分
    let dnsTime = timing.dns.time;
    let dnsScore = baseTimeScoreCalc(dnsTime, 4, 100, 10);
    scoreDetail["DNS resolution time consuming"] = dnsScore / 4;
    initScore += dnsScore;
    
    // tcp连接耗时: 以100ms为基准，超出10ms扣一分，总4分
    let tcpTime = timing.tcp.time;
    let tcpScore = baseTimeScoreCalc(tcpTime, 4, 100, 10);
    scoreDetail["TCP connection time consuming"] = tcpScore / 4;
    initScore += tcpScore;

    // 总加载耗时: 3秒为基准，超出一s扣三分 总分7分
    let loadTime = timing.loaded.time;
    let loadScore = baseTimeScoreCalc(loadTime, 7, 3000, 1000)
    scoreDetail["Total load time"] = loadScore / 7;
    initScore += loadScore;

    // 其他绘制相关的耗时计算:
    let length = auditTime.length;
    for(let i = 0; i < length; i++){
        if(!!baseScoreWithAudit[auditTime[i].title]){
            let baseScore = baseScoreWithAudit[auditTime[i].title];
            initScore += baseScore * auditTime[i].score;
            scoreDetail[auditTime[i].title] = auditTime[i].score;
        }
    }
    return {
        score: initScore,
        detail: scoreDetail
    }
}

function baseTimeScoreCalc(time, baseScore, baseTime, step){
    if(time > baseTime){
        return (time - baseTime) >= baseScore * step ? 0 : baseScore - ((time - baseTime) / step)
    }else{
        return baseScore;
    }
}


const baseScoreWithSource = {
    "Avoid multiple page redirects": 5,  // 避免多页面重定向
    "Defer offscreen images": 5,  // 延迟隐藏图片加载
    "Minify CSS": 5,  // css压缩
    "Minify JavaScript": 5,  // js压缩
    "Remove unused CSS": 5,  // 是否移除无用的css
    "Remove unused JavaScript": 5,  // 是否移除无用的js
    "Serve images in next-gen formats": 5,  // 图片资源是否支持下一代图像: webp等
    "Properly size images": 5,  // 图片资源大小是否适当
    "Remove duplicate modules in JavaScript bundles": 5,  // 是否移除重复的js
    "Use HTTP/2": 5  // 是否使用http 2
}
// 资源加载优化维度计分:
function sourceScoreCalc(opportunity){
    let initScore = 0;
    let scoreDetail = Object.create(baseScoreWithSource);
    let length = opportunity.length;
    for(let i = 0; i < length; i++){
        if(!!baseScoreWithSource[opportunity[i].title]){
            initScore += baseScoreWithSource[opportunity[i].title] * opportunity[i].score;
            scoreDetail[opportunity[i].title] = opportunity[i].score;
        }
    }
    return {
        score: initScore,
        detail: scoreDetail
    }
}


module.exports = {
    calcScore
}